package com.medicinemanager.control;

import com.medicinemanager.app.AppManager;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

public class DoubleClickExit {
	private boolean mExist = false;
	private final int SPACE = 2000;
	private Context mContext = null;
	private Handler mHandle;
	
	/**
	 * 		handle the message which by change the mExist to false
	 * when delay time is SPACE , double click the mExist is true,
	 * it takes long time , it will be false
	 */
	@SuppressLint("HandlerLeak") 
	public DoubleClickExit(Context context){
		mContext = context;
		mHandle = new Handler(){
			public void handleMessage(Message msg){
				mExist = false;
			}
		};
	}
	
	public void exitApp(){
		if(!mExist){
			mExist = true;
			Toast.makeText(mContext, "double click to exit", Toast.LENGTH_SHORT).show();
			mHandle.sendEmptyMessageDelayed(0, SPACE);
		}else{
			System.out.println("in");
			showDialog();
		}
	}
	
	/**
	 * 
	 */
	private void showDialog(){
		new AlertDialog.Builder(mContext)
		.setMessage("make sure exit?")
		.setPositiveButton("SURE", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				AppManager.getInstance().appExit(mContext);
			}
		})
		.setNegativeButton("I WANT TO STAY", null)
		.create()
		.show();
	}
}
